<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
    <style>
        body {
            background-color: #f8f9fa;
        }
        .container {
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            padding: 30px;
            margin-top: 50px;
        }
        h1 {
            text-align: center;
            color: #343a40;
            margin-bottom: 30px;
        }
        table {
            margin-bottom: 30px;
        }
        th, td {
            text-align: center;
        }
        form {
            text-align: center;
            margin-bottom: 20px;
        }
        #PaymentResult {
            text-align: center;
            margin-bottom: 20px;
        }
        a {
            display: block;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>购物车</h1>
        <table class="table table-bordered table-striped">
            <thead class="thead-dark">
                <tr>
                    <th>商品名称</th>
                    <th>价格</th>
                    <th>数量</th>
                    <th>总计</th>
                </tr>
            </thead>
            <tbody>
                {% for item in cart %}
                    <tr>
                        <td>{{ item[0] }}</td>
                        <td>{{ item[1] }}元</td>
                        <td>{{ item[2] }}</td>
                        <td>{{ item[1] * item[2] }}元</td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>

        <!-- 显示总价 -->
        <div class="cart-total">
            总金额：<span id="total-price">{{ total_amount }}</span>元
            <input type="hidden" id="order_id" value="{{ order_id }}" />
            <input type="hidden" id="total_amount" value="{{ total_amount }}" />
        </div>

        <button type="submit" id="PayButton" class="btn btn-primary">支付</button>

        <div id="PaymentResult"></div>
        <a href="{{ url_for('home.goods') }}">
            <button class="btn btn-secondary">商品列表</button>
        </a>
    </div>

    <!-- 引入 jQuery -->
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script>
        $(document).ready(function() {
            // 支付按钮的点击事件
            $('#PayButton').click(function() {
                // 使用AJAX提交支付请求
                $.ajax({
                    url: '/api/v1/payments',  // 假设后端支付处理接口
                    type: 'POST',
                    data: {orderId:$('#order_id').val(), amount:$('#total_amount').val()},
                    success: function(response) {
                        alert('支付成功！');
                        console.log(response);
                        window.location.reload()
                    },
                    error: function(xhr, status, error) {
                        alert('支付失败，请稍后再试。');
                        console.log(error);
                    }
                });
            });
        });
    </script>
</body>
</html>
    